/**********************************************************************************
* CLASS 
*		Genesis
*
* DESCRIPTION
* 		Contend manipulate function set:
*			- setSize
*			- setPosition
*			- addComponent
*			- addComponentOnly
*			- addAnimationOnly
*			- getComponentbyIndex
*			- setComponentIndex  
*			- addCommand
*			- getCommand	
*
* REVISION HISTORY
*		Revision 1.0 2008/08/17 Atthaboon S.
*		First Release.
*		Revision 1.1 2008/08/31 Atthaboon S.
*		Implement Prototype Framework.
*
**********************************************************************************/
var Genesis = Class.create({
	
	/****************************************************
	* FUNCTION 
	*		initialize
	*
	* DESCRIPTION
	* 		initialize the Genesis object.
	*
	* ARGUMENTS
	*		position_x		   	:	Genesis position of x at north left Book corner.
	*		position_y 	   	   	:	Genesis position of y at north left Book corner.
	*
	****************************************************/
  	initialize: function(position_x,position_y)
  	{
  		// Keep animationcontrol command
  		this.animation_container   = new Array(); 
  		this.currentDisplay		   = 0;
		
		// Component
		this.component_container = new Array();	
		this.domObj = new Element('div', { 'style':'position:absolute; left:'+position_x+'px;top:'+position_y+'px;overflow:hidden; width: 300px; height:300px;'});
		this.domObj.style.backgroundColor = "white";

		//document.documentElement.childNodes[1].appendChild(this.domObj);
		
		Thread.addGenesis(this);
		
		this.property = null;
  	},
	
	/****************************************************
	* FUNCTION 
	*		getDomType
	*
	* DESCRIPTION
	* 		Use by Genesis for get Genesis dom Component Type.
	*
	* ARGUMENTS
	*
	****************************************************/
	getDomType : function()
	{
		return "Genesis";
	},
  	
  	/****************************************************
	* FUNCTION 
	*		getDomObj
	*
	* DESCRIPTION
	* 		Use by Genesis for get Genesis dom Component.
	*
	* ARGUMENTS
	*
	****************************************************/
	getDomObj : function()
	{
		return this.domObj;
	},
	
	/****************************************************
	* FUNCTION 
	*		getProperty
	*
	* DESCRIPTION
	* 		Get Image Property
	*
	* ARGUMENTS
	*
	****************************************************/
	generatePropertyBar : function()
	{
		var  GenesisDomObj = $(this.domObj);
	
		this.property = new PropertyBar("Image",600);
		this.width_box       	  = this.property.addTextBox('Width','px','5',0);
		this.width_box.value      = parseInt($(this.domObj).style.width);
		this.width_box.onchange   = function() {
			GenesisDomObj.style.width = this.value+'px';
			setResizeAreaPosition(_dragElement);
		}

		
		this.height_box      	  = this.property.addTextBox('Height','px','5',0);
		this.height_box.value     = parseInt($(this.domObj).style.height);
		this.height_box.onchange   = function() {
			GenesisDomObj.style.height = this.value+'px';
			setResizeAreaPosition(_dragElement);
		}
		
		this.position_x_box  	  = this.property.addTextBox('Positon X','px','5',1);
		this.position_x_box.value = parseInt($(this.domObj).style.left);
		this.position_x_box.onchange   = function() {
			GenesisDomObj.style.left = this.value+'px';
			setResizeAreaPosition(_dragElement);
		}
		
		this.position_y_box  	  = this.property.addTextBox('Positon Y','px','5',1);
		this.position_y_box.value = parseInt($(this.domObj).style.top);
		this.position_y_box.onchange   = function() {
			GenesisDomObj.style.top = this.value+'px';
			setResizeAreaPosition(_dragElement);
		}	
		
		this.opacity_box     	  = this.property.addTextBox('Opacity','','5',0);
		this.opacity_box.value    = parseFloat($(this.domObj).getStyle('opacity'));
		this.opacity_box.onchange   = function() {
			GenesisDomObj.setOpacity(this.value);
			setResizeAreaPosition(_dragElement);
		}		
		
	},
	
	/****************************************************
	* FUNCTION 
	*		getProperty
	*
	* DESCRIPTION
	* 		Get Image Property
	*
	* ARGUMENTS
	*
	****************************************************/	
	getProperty : function()
	{	
		// Is not generate property yet do generate it's
		if(this.property == null)
		{
			this.generatePropertyBar();
		}
		else if(this.property.getDomObj().innerHTML == '')
		{
			this.generatePropertyBar();
		}
		return this.property.getDomObj();
	},	
	
    /****************************************************
	* FUNCTION 
	*		syncProperty
	*
	* DESCRIPTION
	* 		Sync Image Property
	*
	* ARGUMENTS
	*
	****************************************************/	
	syncProperty : function()
	{
		this.width_box.value      = parseInt($(this.domObj).style.width);
		this.height_box.value     = parseInt($(this.domObj).style.height);
		this.position_x_box.value = parseInt($(this.domObj).style.left);
		this.position_y_box.value = parseInt($(this.domObj).style.top);
		this.opacity_box.value    = parseFloat($(this.domObj).getStyle('opacity'));
	},
	
  	/****************************************************
	* FUNCTION 
	*		setSize
	*
	* DESCRIPTION
	* 		Set Genesix size.
	*
	* ARGUMENTS
	*		width		   	:	width in px.
	*		height 	   	   	:	height in px.
	*
	****************************************************/
	setSize : function(width,height)
	{	
		$(this.domObj).setStyle({
		  	width:  width+'px',
		  	height: height+'px'
		});
	},
	
	/****************************************************
	* FUNCTION 
	*		setPosition
	*
	* DESCRIPTION
	* 		Set Genesis Position.
	*
	* ARGUMENTS
	*		position_x		   	:	Genesis position of x at north left Book corner.
	*		position_y 	   	   	:	Genesis position of y at north left Book corner.
	*
	****************************************************/
	setPosition : function(position_x,position_y)
	{
		$(this.domObj).setStyle({
		  	left: position_x+'px',
		  	top: position_y+'px'
		});
	},
	
	/****************************************************
	* FUNCTION 
	*		addComponent
	*
	* DESCRIPTION
	* 		Add component object to Genesis.
	*
	* ARGUMENTS
	*		component		:	Component Object.
	*
	****************************************************/
	addComponent : function(component)
	{
		this.domObj.appendChild(component.getDomObj());
		this.component_container[this.component_container.length] = component;
	},
	
	/****************************************************
	* FUNCTION 
	*		addComponentOnly
	*
	* DESCRIPTION
	* 		Add component object to Genesis for only add dom obj.
	*
	* ARGUMENTS
	*		component		:	Component Object.
	*
	****************************************************/
	addComponentOnly : function(component)
	{
		this.domObj.appendChild(component.getDomObj());
	},
	
	/****************************************************
	* FUNCTION 
	*		addAnimationOnly
	*
	* DESCRIPTION
	* 		Add component object to Genesis for only run animation not for add dom obj.
	*
	* ARGUMENTS
	*		component		:	Component Object.
	*
	****************************************************/
	addAnimationOnly : function(component)
	{
		this.component_container[this.component_container.length] = component;
	},
	
	/****************************************************
	* FUNCTION 
	*		addCommand
	*
	* DESCRIPTION
	* 		Add command in the command container for execute.
	*
	* ARGUMENTS
	*		command		   	:	String of command
	*
	****************************************************/
	addCommand : function(command)
	{	
        this.animation_container[this.animation_container.length] = command;
	},
	
	/****************************************************
	* FUNCTION 
	*		removeCommand
	*
	* DESCRIPTION
	* 		Remove command in the command container for execute.
	*
	* ARGUMENTS
	*		index		   	        :	index of command that you want to remove
	*
	****************************************************/
	removeCommand : function(index)
	{	
        array_remove(this.animation_container,index);
	},    
	
	/****************************************************
	* FUNCTION 
	*		getCommand
	*
	* DESCRIPTION
	* 		Get command frome the command container for execute.
	*
	* ARGUMENTS
	*		index		   	:	index of command that you want to get.
    *
    * RETURN
	*       command
	****************************************************/
	getCommand : function(index)
	{	
        if(this.animation_container.length > index && index >= 0)
        {
            return this.animation_container[index];
        }
        return ;
	},
	
    /****************************************************
	* FUNCTION 
	*		getCommandSize
	*
	* DESCRIPTION
	* 		Get size frome the command container for execute.
	*
	* ARGUMENTS
    *
    * RETURN
    *       size    	:        size of command container.
    *
	****************************************************/
	getCommandSize : function()
	{	
        return this.animation_container.length;
	},	
	
	/****************************************************
	* FUNCTION 
	*		executeCommandbyLoop
	*
	* DESCRIPTION
	* 		Execute command frome the command container by looping index.
    *
	****************************************************/
	executeCommandbyLoop : function()
	{	
		if(this.animation_container.length == 0)
		{
			return;
		}
		for(var j=0;j<this.animation_container[0].getSize();j++)
		{
			var animeObj = this.animation_container[0].getCommand(j);
			// verify command that have command for run.
			if(animeObj.getCommand() != "")
			{
				var cmd    = animeObj.getID()+"."+animeObj.getCommand();
				var result = eval(cmd);
				// Finish animation
				if(result == true)
				{
					this.animation_container[0].removeCommand(j);
					j--;
				}
				else
				{
					this.animation_container[0].getCommand(j).setCommand(result);
				}
			}
			// if no command for run re run animation for set command for running.
			else
			{
				var temp_command = eval(animeObj.getAnimation());
				if(temp_command != "")
				{
					animeObj.setCommand(temp_command);
					var cmd = animeObj.getID()+"."+animeObj.getCommand();
					var result = eval(cmd);
					// Finish animation
					if(result == true)
					{
						this.animation_container[0].removeCommand(j);
						j--;
					}
					else
					{
						this.animation_container[0].getCommand(j).setCommand(result);
					}
				}			
			}
		}
		if(this.animation_container[0].getSize() == 0)
		{
			this.removeCommand(0);
		}
	}
});

